package com.zk.algorithm.array;

import com.zk.algorithm.Utils;
import com.zk.algorithm.annotation.Easy;
import com.zk.algorithm.annotation.LeetCodeExplore;

/**
 * @author zk
 */
@Easy
@LeetCodeExplore
public class PlusOne {

    // [4,3,2,1] => [4,3,2,2]
    //
    public int[] plusOne(int[] digits) {
        boolean over10 = false;
        int[] ans = digits;

        for (int i = digits.length - 1; i >= 0; i--) {
            if (i == digits.length - 1 || over10) {
                int plusOne = digits[i] + 1;

                if (over10 = (plusOne >= 10)) {
                    digits[i] = plusOne - 10;
                } else {
                    digits[i] = plusOne;
                }
            }
        }

        if (over10) {
            ans = new int[digits.length + 1];
            System.arraycopy(digits, 0, ans, 1, digits.length);
            ans[0] = 1;
        }

        return ans;
    }

    public static void main(String...args) {
        Utils.println(new PlusOne().plusOne(new int[]{ 4,3,2,1 }));
        Utils.println(new PlusOne().plusOne(new int[]{ 0 }));
        Utils.println(new PlusOne().plusOne(new int[]{ 9,9,9 }));
    }

}
